package com.bigger.controller;

import com.bigger.dto.LoginDTO;
import com.bigger.dto.RegisterDTO;
import com.bigger.dto.UpdatePasswordDTO;
import com.bigger.entity.User;
import com.bigger.properties.JwtProperties;
import com.bigger.result.Result;
import com.bigger.service.UserService;
import com.bigger.utils.JwtUtil;
import com.bigger.vo.LoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;
    @PostMapping("/login")
    public Result<LoginVO> login(@RequestBody LoginDTO loginDTO){
        User user=userService.login(loginDTO);
        Map<String,Object> map=new HashMap<>();
        map.put("id", user.getId());
        String token = JwtUtil.createToken(jwtProperties.getUserKey(), jwtProperties.getUserTtl(), map);
        LoginVO loginVO=new LoginVO(user.getId(),user.getName(),user.getUsername(),token);
        return Result.success(loginVO);
    }
    @PostMapping("/register")
    public Result<String> register(@RequestBody RegisterDTO registerDTO){
        userService.register(registerDTO);
        return Result.success("注册成功");
    }
    @PutMapping("/updatePassword")
    public Result<String> putPassword(@RequestBody UpdatePasswordDTO updatePasswordDTO){
        userService.updatePassword(updatePasswordDTO);
        return Result.success("改密成功");
    }
    @GetMapping("/get")
    public Result<User> getById(){
        User user=userService.getById();
        return Result.success(user);
    }
    //TODO 需要一个修改用户自身信息的DTO
}
